<?php

	/* Note: This thumbnail creation script requires the GD PHP Extension.  
		If GD is not installed correctly PHP does not render this page correctly
		and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
	 */
  include('../init.php');

  $image = new image();
  
	$showcase_id = $_POST["showcase_id"];

	ini_set("html_errors", "0");

	// Check the upload
	if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
		echo "ERROR:invalid upload";
		exit(0);
	}

	$name = get_file_name($showcase_id, $_FILES['Filedata']['tmp_name']);
  $path = "../../images/showcase/$showcase_id";
  
  if(!file_exists($path)){
	mkdir($path);
  }
  $urlpath = get_url_path($path);
  $link = URL."/".$urlpath."/".$name;
  $filename = $path ."/". $name;

  if (!move_uploaded_file($_FILES['Filedata']['tmp_name'], $filename)){
    echo 'Problem: Could not move file to destination directory';
    exit(0);
  }

  ini_set('memory_limit', '100M');
  $image->load($filename);
  $image->resize(414,292);
  $image->save($filename);
  ini_set('memory_limit', '16M');

  chmod($filename,0755);

	if (!isset($_SESSION["file_info"])) {
		$_SESSION["file_info"] = array();
	}

	// Use a output buffering to load the image into a variable
	ob_start();
  $image->output();
  $image->destroy();
	$imagevariable = ob_get_contents();
	ob_end_clean();

  $sql = "INSERT INTO showcase_img(showcase_id,showcase_img_path,showcase_img_show) VALUES('$showcase_id','$name','1')";
  $db->execute($sql);

  $file_id = md5($filename + rand()*100000);

  $_SESSION["file_info"][$file_id] = $imagevariable;

  echo "FILEID:" . $file_id; // Return the file id to the script
?>